package com.ymatou.iapi.prodstock.testcase;

import com.ymatou.iapi.prodstock.parameter.RepairProductStockBean;
import com.ymatou.iapi.prodstock.service.RepairProductStockCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ActivityCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 修复商品总库存 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_RepairProductStock {
	private static RepairProductStockBean repairproductstockBean;
	private static RepairProductStockCall repairproductstockCall;
	private static String sellerName = EnvSetup.getData("autotest_sellerName1");
	private static int sellerid = Integer.parseInt(EnvSetup.getData("autotest_seller1"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修复商品总库存");
	}

	@Before
	public void caseUp() {
		repairproductstockBean = new RepairProductStockBean();
		repairproductstockCall = new RepairProductStockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(repairproductstockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	//手工校验-有延时
	public void Tc_RepairProductStock_001() {
		Logger.start(true, "修复商品总库存");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerid);
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			ProductManagerCallServiceV3.productMongocrudSync(productId, "CreateProduct", 0);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			String activityId = ActivityCallService.createNoIsolationActivityInProcess().toString();
			Logger.comment("创建非交易隔离活动:" + activityId);
//			Thread.sleep(5000);
			// 报名活动 获得商品在活动中的id
			Integer acStock = 100;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerid, catalogs,
					Integer.valueOf(activityId), acStock, "10");
			// 审核通过
			
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			Thread.sleep(5000);
			Integer stock1 = new YmtProductsIWapper().getiTotalStock(productId);
			Integer iNum1 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Integer iNum2 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(1)).getiNum();
			Integer acNum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Integer acNum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();
			//沒有调用过这个接口可能总数为0
//			Logger.verifyEquals(catalogNum * 2, stock1, "验证商品总库存");
			Logger.verifyEquals(acStock, acNum1, "验证商品活动库存");
			Logger.verifyEquals(acStock, acNum2, "验证商品活动库存");
			// 这里的规格数量是库存扣的
			Logger.verifyEquals(catalogNum - acStock, iNum1, "验证商品规格库存");
			Logger.verifyEquals(catalogNum - acStock, iNum2, "验证商品规格库存");

			
//            Logger.verifyEquals(0,stock,"验证总库存");
			int updatecount = 20;
			new YmtProductActivityStockWapper()
					.selectAllBy(MapUtil.hashMap("sproductid", productId, "iactivityid", activityId)).forEach(map -> {
						Logger.debug("i:" + new YmtProductActivityStockWapper().updateiStockNumByProductActivityStockId(
								map.get("sProductActivityStockID").toString(), updatecount));

					});
			repairproductstockBean.setAppId("job.iapi.ymatou.com");
			repairproductstockCall.setData(repairproductstockBean);
			repairproductstockCall.callService();
			Thread.sleep(5000);

			acNum1 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			acNum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(1)).getIstocknum();

			Logger.verifyEquals(updatecount, acNum1, "验证商品活动库存");
			Logger.verifyEquals(updatecount, acNum2, "验证商品活动库存");
//总数计算有延时
//			stock1 = new YmtProductsIWapper().getiTotalStock(productId);
//			Logger.verifyEquals((catalogNum - (acStock - updatecount)) * 2, stock1, "验证商品总库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}